import { getCurrentInstance } from "vue";

export function useGlobalConfig() {
  const instance = getCurrentInstance();
  if (!instance) {
    console.error("useGlobalConfig 只能在setup 中使用");
  }

  return instance?.appContext.config.globalProperties.$SAELEMENT || {};
}

const EventBus:Record<string, (()=>any)[]> = {};
export function useEventHub() {
  return {
    on(key:string, cb:(...args:any[])=>any) {
      if (EventBus[key]) {
        EventBus[key].push(cb);
      } else {
        EventBus[key] = [ cb ];
      }
    },
    emit(key:string, ...args:any[]) {
      EventBus[key].forEach((cb:(...args:any[])=>void) => {
        cb(...args);
      });
    }
  };
}